/*--------------------------------------------------------
CHAT UI ROUTINE
--------------------------------------------------------*/
var windowFocus = true;
var username;
var originalTitle;
var blinkOrder = 0;

var chatboxFocus = new Array();
var newMessages = new Array();
var newMessagesWin = new Array();
var chatBoxes = new Array();
originalTitle = document.title;

$(document).ready(function() {
	$([window, document]).blur(function(){
		windowFocus = false;
	}).focus(function(){
		windowFocus = true;
		document.title = originalTitle;
	});
});

function privateChat(user) {
	var chatbox = createChatBox(user);
	$("#chatbox_" + user.id + " .chatbox-textarea").focus();
}	
		
function restructureChatBoxes() {
	align = 0;
	for (x in chatBoxes) {
		title = chatBoxes[x];

		if ($("#chatbox_" + title).css('display') != 'none') {
			if (align == 0) {
				$("#chatbox_" + title).css('right', '20px');
			} else {
				width = (align) * (225 + 7) + 20;
				$("#chatbox_" + title).css('right', width + 'px');
			}
			align++;
		}
	}
}

function createChatBox(user, isMinimized) {
	var uid = user.id;
	var	uname = user.username;
	
	if ($("#chatbox_" + uid).length > 0) {
		if ($("#chatbox_" + uid).css('display') == 'none') {
			$("#chatbox_" + uid).css('display', 'block');
			restructureChatBoxes();
		}
		$("#chatbox_" + uid + " .chatbox-textarea").focus();
		return;
	}

	$(" <div />").attr("id", "chatbox_" + uid)
	.addClass("chatbox")
	.html('<div class="chatbox-head">' + 
		'<div class="chatbox-title">' + uname + '</div>' + 
		'<div class="chatbox-options">' + 
			'<a href="javascript:void(0)" onclick="javascript:toggleChatBox(\'' + uid + '\')">-</a> ' + 
			'<a href="javascript:void(0)" onclick="javascript:closeChatBox(\'' + uid + '\')">X</a>' + 
		'</div>' + 
		'<br clear="all"/></div>' + 
		'<div class="chatbox-content"></div>' + 
		'<div class="chatbox-input"><textarea class="chatbox-textarea" ' + 
			'onkeydown="javascript:return sendChatMessage(event, this, \'' + uid + '\');">' + 
		'</textarea></div>')
	.appendTo($('body'));
			   
	$("#chatbox_" + uid).css('bottom', '0px');
	
	chatBoxeslength = 0;
	for (x in chatBoxes) {
		if ($("#chatbox_" + chatBoxes[x]).css('display') != 'none') {
			chatBoxeslength++;
		}
	}

	if (chatBoxeslength == 0) {
		$("#chatbox_" + uid).css('right', '20px');
	} else {
		width = (chatBoxeslength) * (225 + 7) + 20;
		$("#chatbox_" + uid).css('right', width + 'px');
	}
	
	chatBoxes.push(uid);
	if (isMinimized == 1) {
		minimizedChatBoxes = new Array();

		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}
		
		minimize = 0;
		for (j = 0; j < minimizedChatBoxes.length; j++) {
			if (minimizedChatBoxes[j] == uid) {
				minimize = 1;
			}
		}

		if (minimize == 1) {
			$('#chatbox_' + uid +' .chatboxcontent').css('display', 'none');
			$('#chatbox_' + uid +' .chatboxinput').css('display', 'none');
		}
	}

	chatboxFocus[uid] = false;

	$("#chatbox_" + uid + " .chatbox-textarea").blur(function(){
		chatboxFocus[uid] = false;
		$("#chatbox_" + uid + " .chatbox-textarea").removeClass('selected');
	}).focus(function() {
		chatboxFocus[uid] = true;
		newMessages[uid] = false;
		$("#chatbox_" + uid + " .chatbox-textarea").addClass('selected');
	});

	$("#chatbox_" + uid).click(function() {
		if ($('#chatbox_' + uid+' .chatbox-content').css('display') != 'none') {
			$("#chatbox_" + uid+" .chatbox-textarea").focus();
		}
	});

	$("#chatbox_" + uid).show();
}

function closeChatBox(uid) {
	$('#chatbox_' + uid).css('display','none');
	restructureChatBoxes();
}

function toggleChatBox(uid) {
	if ($('#chatbox_' + uid + ' .chatbox-content').css('display') == 'none') {  		
		var minimizedChatBoxes = new Array();
		
		if ($.cookie('chatbox_minimized')) {
			minimizedChatBoxes = $.cookie('chatbox_minimized').split(/\|/);
		}

		var newCookie = '';

		for (i = 0; i < minimizedChatBoxes.length; i++) {
			if (minimizedChatBoxes[i] != uid) {
				newCookie += uid + '|';
			}
		}

		newCookie = newCookie.slice(0, -1)


		$.cookie('chatbox_minimized', newCookie);
		$('#chatbox_' + uid + ' .chatbox-content').css('display', 'block');
		$('#chatbox_' + uid + ' .chatbox-input').css('display', 'block');
		$('#chatbox_' + uid + ' .chatbox-content').scrollTop($("#chatbox_" + uid + " .chatbox-content")[0].scrollHeight);
	} else {		
		var newCookie = uid;
		if ($.cookie('chatbox_minimized')) {
			newCookie += '|' + $.cookie('chatbox_minimized');
		}

		$.cookie('chatbox_minimized', newCookie);
		$('#chatbox_' + uid + ' .chatbox-content').css('display', 'none');
		$('#chatbox_' + uid + ' .chatbox-input').css('display', 'none');
	}	
}

function sendChatMessage(event, textarea, uid) {	 
	if(event.keyCode == 13 && event.shiftKey == 0)  {
		msg = $(textarea).val();
		msg = msg.replace(/^\s+|\s+$/g,"");

		$(textarea).val('');
		$(textarea).focus();
		$(textarea).css('height', '44px');
		if (msg != '') {
			// send message to server
			$(document).get(0).CHAT_SOCKET.send({ 
				command: 'privateMessageRequest', 
				recipientId: uid,
				message: msg,
			});
		}
		
		return false;
	}

	var adjustedHeight = textarea.clientHeight;
	var maxHeight = 94;

	if (maxHeight > adjustedHeight) {
		adjustedHeight = Math.max(textarea.scrollHeight, adjustedHeight);
		if (maxHeight)
			adjustedHeight = Math.min(maxHeight, adjustedHeight);
		if (adjustedHeight > textarea.clientHeight)
			$(textarea).css('height',adjustedHeight+8 +'px');
	} else {
		$(textarea).css('overflow','auto');
	}
}	